// +build !shanghai

package global

import (
	"encoding/json"
	"fmt"
	"gamesdk/common/log"
	"gamesdk/common/redis"
	"gamesdk/registry"
	"io/ioutil"
	"os"
	"runtime"
	"strconv"

	"github.com/sipt/GoJsoner"
)

// var (
// 	buildTime = C.GoString(C.build_time())
// )

func BuildTime() string {
	return "v2.0.0"
}

//公共常量
const (
	G_Token = "Authorization"
	G_Path  = true
)

var (
	G_Config *configbody = &configbody{}
)

type configbody struct {
	ServerName   string  `json:"server_name"`
	Runmode      string  `json:"runmode"`
	GameType     int     `json:"game_type"`
	Robot        [][]int `json:"robot"`
	TableNum     int     `json:"table_num"`
	Max          int     `json:"max"`
	Min          int     `json:"min"`
	Consul       string  `json:"consul"`
	GameId       int64   `json:"game_id"`
	RedisCluster int
}

func init() {
	fmt.Println("************************************************")
	fmt.Println("*                                              *")
	fmt.Println("*            Game Frame Run System !           *")
	fmt.Println("*                                              *")
	fmt.Println("************************************************\n")

	fmt.Println("### BUILD: ", BuildTime())
	fmt.Println("### VER: ", "3.0.0")
	fmt.Println("### PID: ", os.Getpid())
	fmt.Println("### Path: ", G_Path)
	runtime.GOMAXPROCS(runtime.NumCPU())
	gameConfigData, err := ioutil.ReadFile("./config/game_frame.json")
	if err != nil {
		panic(err.Error())
		return
	}
	gameConfigResult, err := GoJsoner.Discard(string(gameConfigData))
	if err != nil {
		panic(err.Error())
		return
	}
	err = json.Unmarshal([]byte(gameConfigResult), G_Config)
	if err != nil {
		panic(err.Error())
		return
	}
	consul := os.Getenv("CONSUL")
	if consul != "" {
		G_Config.Consul = consul
	}
	gameID := os.Getenv("GAMEID")
	if gameID != "" {
		gameId, err := strconv.ParseInt(gameID, 10, 64)
		if err != nil {
			G_Config.GameId = gameId
		}
	}

	table_num_s := os.Getenv("TABLE_NUM")
	table_num, err := strconv.Atoi(table_num_s)
	if err == nil {
		if table_num > 0 {
			G_Config.TableNum = table_num
		}
	}

	runmode := os.Getenv("RUNMODE")
	if runmode != "" {
		G_Config.Runmode = runmode

	}

	max_s := os.Getenv("MAX")
	max, err := strconv.Atoi(max_s)
	if err == nil {
		if max > 0 {
			G_Config.Max = max
		}
	}
	G_Config.RedisCluster = 0
	fmt.Println("### Consul: ", G_Config.Consul)
	fmt.Println("### GameId: ", G_Config.GameId)
	fmt.Println("### Game: ", G_Config.ServerName)
	fmt.Println("### TableNum: ", G_Config.TableNum)
	fmt.Println("### Max: ", G_Config.Max)
	fmt.Println("### Min: ", G_Config.Min)
	fmt.Println("### Runmode: ", G_Config.Runmode)
	err = registry.Init(G_Config.Consul)
	if err != nil {
		panic(err)
		return
	}

	redis_ip, redis_pass, redis_port, err := registry.ServiceRedis()
	if err != nil {
		panic(err.Error())
		return
	}
	redis.InitRedisPass(fmt.Sprintf("%s:%d", redis_ip, redis_port), redis_pass)

	// kafka_ip, kafka_port, err := registry.ServiceKafka()
	// if err != nil {
	// 	panic(err.Error())
	// 	return
	// }
	//producer.InitProducer([]string{fmt.Sprintf("%s:%d", kafka_ip, kafka_port)})
	funcInitLog()
	//go record.RunProducer()
}

//初始化日志
func funcInitLog() {
	if G_Config.Runmode == "dev" {
		log.SetLogLevel(log.LEVEL_DEBUG)
		log.SetLogModel(log.MODEL_DEV)
	} else if G_Config.Runmode == "info" {
		log.SetLogLevel(log.LEVEL_INFO)
		log.SetLogModel(log.MODEL_INFO)
	} else {
		log.SetLogLevel(log.LEVEL_WARNING)
		log.SetLogModel(log.MODEL_PRO)
	}
	if ip, port, e := registry.ServiceElk(); e == nil {
		log.Info("启动日志收集 %s", fmt.Sprintf("http://%s:%d", ip, port))
		//log.OpenSendLog(G_Config.ServerName, true, fmt.Sprintf("http://%s:%d", ip, port))
	} else {
		log.Warn("不启动集中日志处理")
	}
}
